package com.chuanggou.util;

import com.chuanggou.file.FastDFSFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;

import org.junit.Test;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;

import java.io.InputStream;

/**
 * @author yd
 * @version 1.0
 * @date 2020/2/25 0025 黄啟军
 * 实现FastDFS文件管理
 * 文件上传/删除/下载/文件信息获取/
 * Storage信息获取/Tracker信息获取///
 */
public class FastDFSUtil {
    /**加载Tracker链接信息*/
    static{
        try {
            //查找classpath下的文件路径
            String path = new ClassPathResource("fdfs_client.conf").getPath();
            //加载配置文件的信息
            ClientGlobal.init(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**删除文件*/
    public static void deleteFile(String groupName, String remoteFileName) throws Exception{
        //创建一个Tracker访问的客户端对象TrackerClient
        TrackerClient trackerClient = new TrackerClient();
        //通过TrackerClient访问TrackerServer服务，获取连接信息
        TrackerServer trackerServer = trackerClient.getConnection();
        /**通过TrackerServer链接信息可以获取Storage的链接信息，创建
         * StorageClient对象存储Storage的链接信息
         */
        StorageClient storageClient = new StorageClient(trackerServer, null);
        //删除文件
        int i = storageClient.delete_file(groupName, remoteFileName);
    }
    /**文件删除测试*/
    @Test
    public void test02() throws Exception {
        deleteFile("group1","M00/00/00/wKgrcV5VFoyECqlAAAAAAJQNpEQ800.jpg");
    }
    /**文件下载
     *  /*groupName:文件的组名
     *   * remoteFileName 文件的存储路径名字 M00/00/00/wKgrcV5VFoyECqlAAAAAAJQNpEQ800.jpg
     * */
    public InputStream downloadFile(String groupName, String remoteFileName)throws Exception{
        //创建一个Tracker访问的客户端对象TrackerClient
        TrackerClient trackerClient = new TrackerClient();
        //通过TrackerClient访问TrackerServer服务，获取连接信息
        TrackerServer trackerServer = trackerClient.getConnection();
        /**通过TrackerServer链接信息可以获取Storage的链接信息，创建
         * StorageClient对象存储Storage的链接信息
         */
        StorageClient storageClient = new StorageClient(trackerServer, null);
        //文件下载
        byte[] bytes = storageClient.download_file(groupName, remoteFileName);
        return new ByteArrayInputStream(bytes);
    }
    /**模拟下载*/
    @Test
    public void test() throws Exception {
        InputStream is =
                downloadFile("group1",
                        "M00/00/00/wKgrcV5VFoyECqlAAAAAAJQNpEQ800.jpg");
        //将文件写入到本地磁盘
        FileOutputStream os = new FileOutputStream("D:/1.jpg");
        //定义缓冲区
        byte[] buffer = new byte[1024];
        while (is.read(buffer)!=-1){
            os.write(buffer);
        }
        //刷新缓冲区
        os.flush();
        //关闭io流
        os.close();
        is.close();
    }
    /**文件上传
     * 封装文件信息
     * */
    public static String[] upload(FastDFSFile fastDFSFile)throws Exception{
        //附加参数
        NameValuePair[] meta_list = new NameValuePair[1];
        meta_list[0] = new NameValuePair("拍摄地址",fastDFSFile.getAuthor());
        //创建一个Tracker访问的客户端对象TrackerClient
        TrackerClient trackerClient = new TrackerClient();
        //通过TrackerClient访问TrackerServer服务，获取连接信息
        TrackerServer trackerServer = trackerClient.getConnection();
        /**通过TrackerServer链接信息可以获取Storage的链接信息，创建
         * StorageClient对象存储Storage的链接信息
        */
        StorageClient storageClient = new StorageClient(trackerServer, null);
        //通过StorageClient访问Storage,实现文件上传，并且获取文件上传后的存储信息
        /**参数说明1：上传文件的字节数组
         * 2.文件的扩展名 jpg
         * 3.附加参数，比如:拍摄地址：北京
         * uploads的两个参数r
         * 1.uploads[0]:文件上传所存储的Storage的组名字
         * 2.uploads[1]:文件存储到Storage上的文件名字 M00/02/44/文件名
         * */
        String[] uploads = storageClient.upload_appender_file(fastDFSFile.getContent(), fastDFSFile.getExt()
                , meta_list);
        return uploads;
    }
    /**获取文件信息
     * groupName:文件的组名
     * remoteFileName 文件的存储路径名字 M00/00/00/wKgrcV5VFoyECqlAAAAAAJQNpEQ800.jpg
     * */
    public static FileInfo getFile(String groupName, String remoteFileName) throws Exception {
        //创建一个TrackerClient对象，通过TrackerClient对象访问TrackerServer
        TrackerClient trackerClient = new TrackerClient();
        //通过TrackerClient获取TrackerServer的链接对象
        TrackerServer trackerServer = trackerClient.getConnection();
        //通过TrackerServer获取Storage信息，创建StorageClient对象存储Storage信息
        StorageClient storageClient = new StorageClient(trackerServer, null);
        //获取文件信息
        return storageClient.get_file_info(groupName,remoteFileName);
    }
   /**测试获取的文件信息*/
    public static void main(String[] args) throws Exception {
        FileInfo fileInfo = getFile("group1",
                "M00/00/00/wKgrcV5VFoyECqlAAAAAAJQNpEQ800.jpg");
        //IP
        fileInfo.getSourceIpAddr();
        System.out.println(fileInfo.getSourceIpAddr());
        //文件大小
        System.out.println(fileInfo.getFileSize());
    }
    /**获取Storage信息*/
    public static StorageServer getStorages()throws Exception{
        //创建一个TrackerClient对象，通过TrackerClient对象访问TrackerServer
        TrackerClient trackerClient = new TrackerClient();
        //通过TrackerClient获取TrackerServer的链接对象
        TrackerServer trackerServer = trackerClient.getConnection();
        //获取Storage信息
        StorageServer storage = trackerClient.getStoreStorage(trackerServer);
        return storage;
    }
    @Test
    public  void test04() throws Exception {
        StorageServer storages = getStorages();
        //获取下标
        int index = storages.getStorePathIndex();
        System.out.println(index);
        //获取ip
        System.out.println(storages.getInetSocketAddress().getHostString());
        //测试Tracker
        getTrackerInfo();
    }
    /**获取Tracker信息*/
    public static String  getTrackerInfo()throws Exception{
        //创建一个TrackerClient对象，通过TrackerClient对象访问TrackerServer
        TrackerClient trackerClient = new TrackerClient();
        //通过TrackerClient获取TrackerServer的链接对象
        TrackerServer trackerServer = trackerClient.getConnection();
        //Tracker的IP，http端口
        int port = ClientGlobal.getG_tracker_http_port();//8080
        String ip = trackerServer.getInetSocketAddress().getHostString();//获取IP
        System.out.println("ip:"+ip+"端口："+port);
        String url = "http://"+ip+":"+port;
        return url;
    }

}
